import 'package:dio/dio.dart';
import 'package:mall/core/models/order_detail.dart';
import 'package:mall/core/services/api.dart';
import 'package:mall/core/providers/view_model.dart';

class OrderDetailModel extends ViewModel {
  final Api api;
  final int storeId;
  final String code;
  String _errorMessage;
  OrderDetail _orderDetail;

  OrderDetailModel(this.api, this.storeId, this.code) : super() {
    _initData();
  }

  String get errorMessage => _errorMessage;

  OrderDetail get orderDetail => _orderDetail;

  Future<ViewState> _initData() async {
    viewState = ViewState.busy;
    try {
      _orderDetail = await api.orderDetail(storeId, code, cancelToken);
      viewState = ViewState.success;
    } catch (e) {
      _errorMessage = e is DioError ? e.message : e.toString();
      viewState = ViewState.error;
    }
    return viewState;
  }
}
